<?php
require 'cache.php';
class CacheDemo extends Cache{

    public function __construct()
    {
        session_start();
    }

    /*
     * 设置session
     * */
    public function set($key, $value, $expire=6000)
    {
        $session_data = [];
        $session_data['data'] = $value;
        $session_data['expire'] = time() + $expire;
        $_SESSION[$key] = $session_data;
        // TODO: Implement set() method.
        echo '缓存成功';
    }

    /*
     * 读取session
     * */
    public function get($key)
    {
        if(isset($_SESSION[$key])){
            //就证明还没有过期
            if($_SESSION[$key]['expire'] > time()){
                return $_SESSION[$key]['data'];
            }else{
                return $this->del($key);
            }
        }
        return '次缓存不存在';
        // TODO: Implement get() method.
    }

    public function del($key)
    {
        if(isset($_SESSION[$key])){
           unset($_SESSION[$key]);
        }else{
            return json_encode([-1=>'此缓存不存在']);
        }
        // TODO: Implement del() method.
    }

    //删除所有的缓存
    public function delAll()
    {
        session_destroy();
        // TODO: Implement delAll() method.
    }

    public function has($key)
    {
        if(isset($_SESSION[$key])){
            return true;
        }else{
            return false;
        }
        // TODO: Implement has() method.
    }


}
